﻿#include "inc.h"
#include "GWindow.h"
#include "GRender.h"
#include "GShader.h"
#include "GCamera.h"
#include "GScene.h"
#include "GTexture.h"

static double get_native_time(void) {
	static double period = -1;
	LARGE_INTEGER counter;
	if (period < 0) {
		LARGE_INTEGER frequency;
		QueryPerformanceFrequency(&frequency);
		period = 1 / (double)frequency.QuadPart;
	}
	QueryPerformanceCounter(&counter);
	return counter.QuadPart * period;
}

float platform_get_time(void) {
	static double initial = -1;
	if (initial < 0) {
		initial = get_native_time();
	}
	return (float)(get_native_time() - initial);
}

int main(void)
{
	Gdiplus::GdiplusStartupInput gdiplusstartupinput;
	ULONG_PTR gdiplustoken;
	Gdiplus::GdiplusStartup(&gdiplustoken, &gdiplusstartupinput, NULL);

	GWindow* pWindow = new GWindow(800, 600);
	pWindow->Init();

	GCamera* pCamera = new GCamera(Vector3f(0, 1, 5), Vector3f(0, 1, 0), Vector3f(0, 1, 0), 1.333f);
	GRender* pRender = new GRender();
	GScene* pScene = generate_test_scene();

	Payload payload;
	payload.pCamera = pCamera;
	payload.viewport_matrx = pWindow->GenViewPortMatrix();
	payload.pWindow = pWindow;

	pRender->SetRenderTarget(pWindow->GetRenderTarget());
	pRender->ClearRenderTarget();

	int nFrame = 0;
	float fPreTime = platform_get_time();
	GWindow::ms_pEvent->pCurrentWindow = pWindow;

	while (true)
	{
		pWindow->HandleEvent(pCamera);
		pRender->ClearRenderTarget();
		pRender->Render(pScene, payload);
		pWindow->WindowDraw();

		float fCurrentTime = platform_get_time();
		nFrame++;
		if (fCurrentTime - fPreTime >= 1)
		{
			printf("fps %3d\n", nFrame);
			nFrame = 0;
			fPreTime = fCurrentTime;
		}
		pWindow->DispatchMsg();
	}

	return 0;
}